package com.tvunetworks.device.extra.controller;

import com.tvunetworks.center.common.model.LoginUser;
import com.tvunetworks.center.common.model.UpdateTokenCodeParam;
import com.tvunetworks.center.common.util.Result;
import com.tvunetworks.center.common.util.ResultUtil;
import com.tvunetworks.device.extra.annotation.Logined;
import com.tvunetworks.device.extra.model.param.DeletePairParam;
import com.tvunetworks.device.extra.model.param.GetCodeParam;
import com.tvunetworks.device.extra.service.AnywhereService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author Etan Yang
 * @create: 2021/11/22 13:54
 */

@RestController
@RequestMapping("/tokenPair")
public class TokenPairController {
    @Autowired
    private AnywhereService anywhereService;

    /**
     * create token code by param
     *
     * @param loginUser loginUser
     * @param param     UpdateTokenCodeParam
     * @return tokenCode
     */
    @PostMapping("createTokenCode")
    public Result createTokenCode(@Logined LoginUser loginUser, @RequestBody UpdateTokenCodeParam param) {
        String userId = StringUtils.isNotEmpty(param.getUserId()) ? param.getUserId() : loginUser.getId();
        param.setUserId(userId);
        return anywhereService.createTokenCode(param);
    }

    /**
     * 分页查询code码
     *
     * @param loginUser 登录用户
     * @param param param
     * @return code码的集合
     */
    @PostMapping("getCode")
    public Result getCode(@Logined LoginUser loginUser, @RequestBody GetCodeParam param) {
        String userId = StringUtils.isNotEmpty(param.getUserId()) ? param.getUserId() : loginUser.getId();
        return ResultUtil.success(anywhereService.getCode(userId));
    }

    /**
     * 删除anywhere和receiver的配对关系
     *
     * @param loginUser loginUser
     * @param param parameter
     * @return 返回结果
     */
    @PostMapping("deletePair")
    public Result deletePair(@Logined LoginUser loginUser, @Validated @RequestBody DeletePairParam param) {
        param.setUserId(loginUser.getId());
        return anywhereService.deletePair(param);
    }

    /**
     * 校验token扫码是否成功
     * @param code code码
     * @return 0表示成功，1表示失败，2表示无响应
     */
    @PostMapping("checkPairStatus")
    public Result checkPairStatus(@RequestParam("code") String code) {
        return ResultUtil.success(anywhereService.checkPairStatus(code));
    }
}
